home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 2002 November / SGI Freeware 2002 November - Disc 3.iso / dist / fw_qt3.idb / usr / freeware / Qt / examples / customlayout / flow.cpp.z / flow.cpp
C/C++ Source or Header  |  2002-04-08  |  3KB  |  134 lines

  1. /****************************************************************************
  2. ** $Id:  qt/flow.cpp   3.0.3   edited Oct 12 12:18 $
  3. **
  4. ** Implementing your own layout: flow example
  5. **
  6. ** Copyright (C) 1996 by Trolltech AS.  All rights reserved.
  7. **
  8. ** This file is part of an example program for Qt.  This example
  9. ** program may be used, distributed and modified without limitation.
  10. **
  11. *****************************************************************************/
  12.  
  13. #include "flow.h"
  14.  
  15. class SimpleFlowIterator :public QGLayoutIterator
  16. {
  17. public:
  18.     SimpleFlowIterator( QPtrList<QLayoutItem> *l ) :idx(0), list(l)  {}
  19.     uint count() const;
  20.     QLayoutItem *current();
  21.     QLayoutItem *next();
  22.     QLayoutItem *takeCurrent();
  23.  
  24. private:
  25.     int idx;
  26.     QPtrList<QLayoutItem> *list;
  27.  
  28. };
  29.  
  30. uint SimpleFlowIterator::count() const
  31. {
  32.     return list->count();
  33. }
  34.  
  35. QLayoutItem *SimpleFlowIterator::current()
  36. {
  37.     return idx < int(count()) ? list->at(idx) : 0;
  38. }
  39.  
  40. QLayoutItem *SimpleFlowIterator::next()
  41. {
  42.     idx++; return current();
  43. }
  44.  
  45. QLayoutItem *SimpleFlowIterator::takeCurrent()
  46. {
  47.     return idx < int(count()) ? list->take( idx ) : 0;
  48. }
  49.  
  50. SimpleFlow::~SimpleFlow()
  51. {
  52.     deleteAllItems();
  53. }
  54.  
  55.  
  56. int SimpleFlow::heightForWidth( int w ) const
  57. {
  58.     if ( cached_width != w ) {
  59.     //Not all C++ compilers support "mutable" yet:
  60.     SimpleFlow * mthis = (SimpleFlow*)this;
  61.     int h = mthis->doLayout( QRect(0,0,w,0), TRUE );
  62.     mthis->cached_hfw = h;
  63.     return h;
  64.     }
  65.     return cached_hfw;
  66. }
  67.  
  68. void SimpleFlow::addItem( QLayoutItem *item)
  69. {
  70.     list.append( item );
  71. }
  72.  
  73. bool SimpleFlow::hasHeightForWidth() const
  74. {
  75.     return TRUE;
  76. }
  77.  
  78. QSize SimpleFlow::sizeHint() const
  79. {
  80.     return minimumSize();
  81. }
  82.  
  83. QSizePolicy::ExpandData SimpleFlow::expanding() const
  84. {
  85.     return QSizePolicy::NoDirection;
  86. }
  87.  
  88. QLayoutIterator SimpleFlow::iterator()
  89. {
  90.     return QLayoutIterator( new SimpleFlowIterator( &list ) );
  91. }
  92.  
  93. void SimpleFlow::setGeometry( const QRect &r )
  94. {
  95.     QLayout::setGeometry( r );
  96.     doLayout( r );
  97. }
  98.  
  99. int SimpleFlow::doLayout( const QRect &r, bool testonly )
  100. {
  101.     int x = r.x();
  102.     int y = r.y();
  103.     int h = 0;        //height of this line so far.
  104.     QPtrListIterator<QLayoutItem> it(list);
  105.     QLayoutItem *o;
  106.     while ( (o=it.current()) != 0 ) {
  107.     ++it;
  108.     int nextX = x + o->sizeHint().width() + spacing();
  109.     if ( nextX - spacing() > r.right() && h > 0 ) {
  110.         x = r.x();
  111.         y = y + h + spacing();
  112.         nextX = x + o->sizeHint().width() + spacing();
  113.         h = 0;
  114.     }
  115.     if ( !testonly )
  116.         o->setGeometry( QRect( QPoint( x, y ), o->sizeHint() ) );
  117.     x = nextX;
  118.     h = QMAX( h,  o->sizeHint().height() );
  119.     }
  120.     return y + h - r.y();
  121. }
  122.  
  123. QSize SimpleFlow::minimumSize() const
  124. {
  125.     QSize s(0,0);
  126.     QPtrListIterator<QLayoutItem> it(list);
  127.     QLayoutItem *o;
  128.     while ( (o=it.current()) != 0 ) {
  129.     ++it;
  130.     s = s.expandedTo( o->minimumSize() );
  131.     }
  132.     return s;
  133. }
  134.